﻿#include "pathpiontwid.h"
#include "ui_pathpiontwid.h"



PathPiontWid::PathPiontWid(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::PathPiontWid())
{
	ui->setupUi(this);
	setStyleSheet("font: 12pt \"Agency FB\"");
	m_fix = new PositionSettingWid(ui->groupBox_5);

	QHBoxLayout* lay1 = new QHBoxLayout();
	lay1->addWidget(m_fix);
	ui->groupBox_5->setLayout(lay1);

	ui->groupBox_5->show();
	ui->groupBox_3->hide();
	ui->groupBox_6->hide();
}

PathPiontWid::~PathPiontWid()
{
	delete ui;
}

void PathPiontWid::Init(const PathPointCfg& cfg)
{
	ui->lineEdit_2->setText(cfg.name());
	ui->comboBox_3->setCurrentIndex(cfg.mathType());
	ui->comboBox->setCurrentIndex(cfg.pointType() - 1);
	ui->checkBox->setTristate(cfg.stop() == 1 ? true : false);
	ui->spinBox->setValue(cfg.vel());
	ui->spinBox_2->setValue(cfg.acc());
	ui->spinBox_3->setValue(cfg.zone());

	ui->comboBox_2->setCurrentIndex(cfg.action().type());
	ui->lineEdit->setText(cfg.action().param());

	m_fix->Init(cfg.fix());
	InitRealiveCfg(cfg.relate());
	
	ui->comboBox_4->setCurrentIndex(cfg.location().type());
	ui->lineEdit_3->setText(cfg.location().id());
}

PathPointCfg PathPiontWid::GetCfg()
{
	PathPointCfg cfg = {};
	cfg.name() = ui->lineEdit_2->text();
	cfg.mathType() = ui->comboBox_3->currentIndex();
	cfg.pointType() = ui->comboBox->currentIndex() + 1;
	cfg.stop() = ui->checkBox->isTristate() ? 1 : 0;
	cfg.vel() = ui->spinBox->value();
	cfg.acc() = ui->spinBox_2->value();
	cfg.zone() = ui->spinBox_3->value();

	cfg.action().type() = ui->comboBox_2->currentIndex();
	cfg.action().param() = ui->lineEdit->text();

	cfg.fix() = m_fix->GetPositon();
	cfg.relate() = GetRelativePointCfg();

	cfg.location().type() = ui->comboBox_4->currentIndex();
	cfg.location().id() = ui->lineEdit_3->text();
	return cfg;
}

void PathPiontWid::on_comboBox_3_currentIndexChanged(int index)
{
	if (index == 0) {	// 固定类型
		ui->groupBox_5->show();
		ui->groupBox_3->hide();
		ui->groupBox_6->hide();
	} else if (index == 1) {	// 相对
		ui->groupBox_5->hide();
		ui->groupBox_3->show();
		ui->groupBox_6->hide();
	} else if (index == 2) {	//抓取点
		ui->groupBox_5->hide();
		ui->groupBox_3->hide();
		ui->groupBox_6->show();
		ui->groupBox_6->setTitle("抓取点");
	} else if (index == 3) {	// 放置点
		ui->groupBox_5->hide();
		ui->groupBox_3->hide();
		ui->groupBox_6->show();
		ui->groupBox_6->setTitle("放置点");
	} else {	// 原点
		ui->groupBox_5->hide();
		ui->groupBox_3->hide();
		ui->groupBox_6->hide();
	}
}


void PathPiontWid::InitRealiveCfg(const RelativePointCfg& cfg)
{
	ui->spinBox_4->setValue(cfg.relateNum());
	//ui->checkBox_x->setTristate(cfg.fix_x());
	ui->checkBox_x->setCheckState(cfg.fix_x() ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
	ui->checkBox_y->setCheckState(cfg.fix_y() ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
	ui->checkBox_z->setCheckState(cfg.fix_z() ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
	ui->checkBox_a->setCheckState(cfg.fix_a() ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
	ui->checkBox_b->setCheckState(cfg.fix_b() ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
	ui->checkBox_c->setCheckState(cfg.fix_c() ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);

	ui->rx->setValue(cfg.diff().x());
	ui->ry->setValue(cfg.diff().y());
	ui->rz->setValue(cfg.diff().z());
	ui->ra->setValue(cfg.diff().a());
	ui->rb->setValue(cfg.diff().b());
	ui->rc->setValue(cfg.diff().c());

}
RelativePointCfg PathPiontWid::GetRelativePointCfg()
{
	RelativePointCfg cfg = {};
	cfg.relateNum = ui->spinBox_4->value();

	cfg.fix_x() = ui->checkBox_x->checkState() == Qt::CheckState::Checked ? true : false;
	cfg.fix_y() = ui->checkBox_y->checkState() == Qt::CheckState::Checked ? true : false;
	cfg.fix_z() = ui->checkBox_z->checkState() == Qt::CheckState::Checked ? true : false;
	cfg.fix_a() = ui->checkBox_a->checkState() == Qt::CheckState::Checked ? true : false;
	cfg.fix_b() = ui->checkBox_b->checkState() == Qt::CheckState::Checked ? true : false;
	cfg.fix_c() = ui->checkBox_c->checkState() == Qt::CheckState::Checked ? true : false;;

	cfg.diff().x() = ui->rx->value();
	cfg.diff().y() = ui->ry->value();
	cfg.diff().z() = ui->rz->value();
	cfg.diff().a() = ui->ra->value();
	cfg.diff().b() = ui->rb->value();
	cfg.diff().c() = ui->rc->value();

	return cfg;
}
